var page = 0
var pageCount = 1
var pageLoading = false
$(function() {
	var name = sessionStorage.getItem("name")
	$(".name").html(name)
	var code = sessionStorage.getItem("code")
		var jpxz1 = sessionStorage.getItem("jpxz")
		var wdxz = sessionStorage.getItem("wdxz")
		var rqxz = sessionStorage.getItem("rqxz")
		$("#jpxz").parents(".cell-item").css("display","none")
		$("#wdxz").val(wdxz)
		$("#rqxz").val(rqxz)
		abb()
	$("#cx").click(function() {
		$("#J_List").find(".list-cont").html("")
		page = 0
		pageCount = 1
		abb()
	})

	$("#J_List").scroll(function() {
		if(pageLoading) {
			return;
		}
		if($('#J_List').scrollTop() + $(document).height() > $('#J_List').find(".list-cont").height() + height4 + height2) {
			abb()
		}
	})
	$("div").delegate(".ck", "click", function() {
		var id_prikey = $(this).attr("id_prikey")
		sessionStorage.setItem("id_prikey", id_prikey)
		var hdh = $(this).attr("hdh")
		sessionStorage.setItem("hdh", hdh)
		window.location.href = "htck"
	})
	$.post("selectCountByPrize", {
		"activityType": code
	}, function(data) {
		data=data.data
		if(data.length > 0) {
			for(var i in data) {
				arr.push([data[i].优惠政策, data[i].人数])
			}
			jptj()
		} else {
			var html = '<div style="height:150px;line-height:150px;text-align:center;font-size:16px;background:white;">暂无数据</div>'
			$("#jptj").html(html)
		}
	})
	$.post("selectCountByDate", {
		"activityType": code
	}, function(data) {
		data=data.data
		if(data.length > 0) {
			for(var i in data) {
				date.push(data[i].时间 + '日')
				cyrs.push(data[i].人数)
			}
			rstj()
		} else {
			var html = '<div style="height:150px;line-height:150px;text-align:center;font-size:16px;background:white;">暂无数据</div>'
			$("#rstj").html(html)
		}
	})
	$.post("selectCountByActivityType", function(data) {
		/*alert(JSON.stringify(data))*/
		data=data.data
		if(data.length > 0) {
			for(var i in data) {
				hdtjdata.push({
					name: data[i].活动名称,
					data: [data[i].人数]
				})
			}
			hdtj()
		} else {
			var html = '<div style="height:150px;line-height:150px;text-align:center;font-size:16px;background:white;">暂无数据</div>'
			$("#hdtj").html(html)
		}
	})
	$.post("selectCountByByDotActivityType", {
		"activityType": code
	}, function(data) {
		data=data.data
		if(data.length > 0) {
			for(var i in data) {
				wdname.push(data[i].name)
				wd.push(data[i].人数)
			}
			$("#wdtj").css("height", data.length * 30 + 200)
			wdtj()
		} else {
			var html = '<div style="height:150px;line-height:150px;text-align:center;font-size:16px;background:white;">暂无数据</div>'
			$("#wdtj").html(html)
		}
	})
})
var arr = []
var date = []
var cyrs = []
var hdtjdata = []
var wdname = []
var wd = []

function jptj() {
	/*alert(arr)*/
	$('#jptj').highcharts({
		chart: {
			plotBackgroundColor: null,
			plotBorderWidth: null,
			plotShadow: false
		},
		title: {
			text: ''
		},
		tooltip: {
			headerFormat: '{series.name}<br>',
			pointFormat: '{point.name}占比: <b>{point.percentage:.1f} %</b><br/>参与人数：{point.y}人'
		},
		credits: {
			enabled: false
		},
		plotOptions: {
			pie: {
				allowPointSelect: true,
				cursor: 'pointer',
				dataLabels: {
					enabled: true,
					format: '{y} 人',
					style: {
						color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
					}
				},
				showInLegend: true
			}
		},
		series: [{
			type: 'pie',
			name: '领取的奖品数量',
			data: arr
		}]
	});
}

function rstj() {
	$('#rstj').highcharts({
		chart: {
			type: 'line'
		},
		title: {
			text: ''
		},
		subtitle: {
			text: ''
		},
		xAxis: {
			categories: date
		},
		yAxis: {
			title: {
				text: '人数 (人)'
			}
		},
		credits: {
			enabled: false
		},
		tooltip: {
			pointFormat: '{series.name}: <b>{point.y}人</b>'
		},
		plotOptions: {
			line: {
				dataLabels: {
					enabled: true // 开启数据标签
				},
				enableMouseTracking: true // 关闭鼠标跟踪，对应的提示框、点击事件会失效
			}
		},
		series: [{
			name: '参与人数',
			data: cyrs
		}]
	});
}

function hdtj() {
	/*alert(JSON.stringify(hdtjdata))*/
	$('#hdtj').highcharts({
		chart: {
			type: 'column'
		},
		title: {
			text: ''
		},
		xAxis: {
			categories: [
				'累计参与人数'
			],
			crosshair: true
		},
		credits: {
			enabled: false
		},
		yAxis: {
			min: 0,
			title: {
				text: '人数(人)'
			}
		},
		tooltip: {
			headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
			pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
				'<td style="padding:0"><b>{point.y} 人</b></td></tr>',
			footerFormat: '</table>',
			shared: true,
			useHTML: true
		},
		plotOptions: {
			column: {
				borderWidth: 0
			}
		},
		series: hdtjdata
	})
}

function wdtj() {
	$('#wdtj').highcharts({
		chart: {
			type: 'bar'
		},
		title: {
			text: ''
		},
		xAxis: {
			categories: wdname,
			title: {
				text: null
			}
		},
		yAxis: {
			min: 0,
			title: {
				text: '参与人数(人)',
				align: 'high'
			},
			labels: {
				overflow: 'justify'
			}
		},
		tooltip: {
			valueSuffix: ' 人'
		},
		plotOptions: {
			bar: {
				dataLabels: {
					enabled: true,
					allowOverlap: true
				}
			}
		},
		credits: {
			enabled: false
		},
		series: [{
			name: '参与人数',
			data: wd
		}]

	});
}

function abb() {
	if(page >= pageCount) {
		return false
	}
	pageLoading = true;
	YDUI.dialog.loading.open("正在加载");
	var code = sessionStorage.getItem("code")
	var name2 = $("#wdxz").val()
	var tel2 = $("#rqxz").val()
	var tel = $("#tel").val()
	sessionStorage.setItem("wdxz", name2)
	sessionStorage.setItem("rqxz", tel2)
	var pageSize = 5
	var pageStart = page * pageSize + 1
	var pageEnd = (page + 1) * pageSize
	page++;
	$.ajax({
		url: 'selectActivityAll',
		dataType: 'json',
		data: {
			start: pageStart,
			end: pageEnd,
			activityType: code,
			ddDept: name2,
			datetime: tel2,
			tel: tel,
		},
		type:'POST',
		success: function(res) {
			YDUI.dialog.loading.close();
			/*alert(JSON.stringify(res))*/
			res=res.data
			var num = res.total
			pageCount = Math.ceil(num / pageSize)
			$(".num").val(num)
			var html = ''
			if(res.rows.length > 0) {
				if(res.rows.length < pageSize) {
					for(var i = 0; i < res.rows.length; i++) {
						html += '<li><h3>姓名：' + res.rows[i].姓名 + '<span>' + res.rows[i].登记时间 + '</span></h3>';
						html += '<p>奖品：' + hb_null(res.rows[i].奖品名称) + '</p>';
						html += '<p>登记人：' + res.rows[i].登记人 + '</p>';
						html += '<h4 class="cell-right cell-arrow"><a id_prikey="' + res.rows[i].id_prikey + '" class="ck" hdh="' + res.rows[i].活动号 + '">查看详情 </a></h4></li>'
					}
					html += '<div class="list-donetip">没有更多数据了</div>'
					$('#J_List').find(".list-cont").append(html);
					pageLoading = false;
				} else {
					for(var i = 0; i < res.rows.length; i++) {
						html += '<li><h3>姓名：' + res.rows[i].姓名 + '<span>' + res.rows[i].登记时间 + '</span></h3>';
						html += '<p>办理业务：' + hb_null(res.rows[i].办理业务) + '</p>';
						html += '<p>登记人：' + res.rows[i].登记人 + '</p>';
						html += '<h4 class="cell-right cell-arrow"><a id_prikey="' + res.rows[i].id_prikey + '" class="ck" hdh="' + res.rows[i].活动号 + '">查看详情 </a></h4></li>'
					}
					$('#J_List').find(".list-cont").append(html);
					pageLoading = false;
				}
			} else {
				html += '<div class="list-donetip">没有更多数据了</div>'
				$('#J_List').find(".list-cont").append(html);
			}
		},
		error: function() {
			pageLoading = false;
		}
	})
}